/* 单例模式 */

/// 数据库连接池，只允许存在一个实例
pub struct ConnectionPool {
    pub host: String,
    pub port: u16,
    pub username: String,
    password: String
}

lazy_static! {
    static ref INSTANCE: ConnectionPool = ConnectionPool{
        host: String::from("localhost"),
        port: 3306,
        username: String::from("root"),
        password: String::from("123456")
    };
}


impl ConnectionPool {

    pub fn get_instance() -> &'static ConnectionPool {
        &INSTANCE
    }

    pub fn get_connection(&self) -> String {
        let conn = format!("使用用户名 {} 登录数据库 {}:{}", self.username, self.host, self.port);
        conn
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_singleton_1() {
        let pool = ConnectionPool::get_instance();
        let conn = pool.get_connection();
        assert_eq!("使用用户名 root 登录数据库 localhost:3306", conn);
    }
}